package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.LocalizedWarning;
import com.ruoyi.system.service.ILocalizedWarningService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 本地预警Controller
 *
 * @author ruoyi
 * @date 2023-10-12
 */
@RestController
@RequestMapping("/system/warning")
public class LocalizedWarningController extends BaseController
{
    @Autowired
    private ILocalizedWarningService localizedWarningService;

    /**
     * 查询本地预警列表
     */
    @PreAuthorize("@ss.hasPermi('system:warning:list')")
    @GetMapping("/list")
    public TableDataInfo list(LocalizedWarning localizedWarning)
    {
        startPage();
        List<LocalizedWarning> list = localizedWarningService.selectLocalizedWarningList(localizedWarning);
        return getDataTable(list);
    }

    /**
     * 导出本地预警列表
     */
    @PreAuthorize("@ss.hasPermi('system:warning:export')")
    @Log(title = "本地预警", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LocalizedWarning localizedWarning)
    {
        List<LocalizedWarning> list = localizedWarningService.exportLocalizedWarningList(localizedWarning);
        ExcelUtil<LocalizedWarning> util = new ExcelUtil<LocalizedWarning>(LocalizedWarning.class);
        util.exportExcel(response, list, "本地预警数据");
    }

    /**
     * 获取本地预警详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:warning:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(localizedWarningService.selectLocalizedWarningById(id));
    }

    /**
     * 新增本地预警
     */
    @PreAuthorize("@ss.hasPermi('system:warning:add')")
    @Log(title = "本地预警", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LocalizedWarning localizedWarning)
    {
        return toAjax(localizedWarningService.insertLocalizedWarning(localizedWarning));
    }

    /**
     * 修改本地预警
     */
    @PreAuthorize("@ss.hasPermi('system:warning:edit')")
    @Log(title = "本地预警", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LocalizedWarning localizedWarning)
    {
        return toAjax(localizedWarningService.updateLocalizedWarning(localizedWarning));
    }

    /**
     * 删除本地预警
     */
    @PreAuthorize("@ss.hasPermi('system:warning:remove')")
    @Log(title = "本地预警", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(localizedWarningService.deleteLocalizedWarningByIds(ids));
    }

    @Log(title = "本地预警", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:warning:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<LocalizedWarning> util = new ExcelUtil<LocalizedWarning>(LocalizedWarning.class);
        List<LocalizedWarning> localizedWarningList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = localizedWarningService.importData(localizedWarningList);
        return success(11);
    }
}
